package com.lsh.payment.core.strategy.payment;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.model.wxpay.WxPayMicropayResult;
import com.lsh.payment.core.strategy.config.WxPayConfig;
import com.lsh.payment.core.strategy.vo.wxpay.WxPayBaseResult;
import com.lsh.payment.core.strategy.vo.wxpay.WxPaySmResponse;
import com.lsh.payment.core.util.BigDecimalUtils;
import com.lsh.payment.core.util.HttpClientUtils;
import com.lsh.payment.core.util.MD5Util;
import com.lsh.payment.core.util.XmlUtil;
import com.lsh.payment.core.util.pay.wxpay.WxSignature;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.lsh.payment.core.constant.BusinessConstant.STRATEGY_BEAN_NAME_PREFIX;
import static com.lsh.payment.core.constant.BusinessConstant.WXBSM;

/**
 * 微信app支付
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 16/10/30
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component(STRATEGY_BEAN_NAME_PREFIX + WXBSM)
public class WxSmPayStrategy implements PayStrategy<WxPaySmResponse> {

    private static Logger logger = org.slf4j.LoggerFactory.getLogger(WxSmPayStrategy.class);

    @Override
    public WxPaySmResponse generatePayParams(int payType, final Map<String, String> paramMap) {

        String wxpaykey = "";
        Map<String, String> wxpayRequestBody = new HashMap<>(18);
        // 检测必填参数
        String payWay = paramMap.get(BusinessConstant.PAY_WAY);
//        if (payWay.equals(PayWay.BSM.getName())) {
//
//            if (paramMap.get("system").equals(CallSystem.GROUPON.getCodeStr())) {
//                wxpaykey = WxPayConfig.GROUPON_KEY;
//                // 应用ID
//                wxpayRequestBody.put("appid", WxPayConfig.GROUPON_APPID);
//                // 商户号
//                wxpayRequestBody.put("mch_id", WxPayConfig.GROUPON_MCHID);
//            } else {
//                logger.info("system para is : " + paramMap.get("system"));
//                return null;
//            }
//
//        } else {
//            logger.info("pay_way para is : " + payWay);
//            return null;
//        }
        logger.info("pay_way para is : " + payWay);
//        return null;
        // 随机字符串
        wxpayRequestBody.put("nonce_str", MD5Util.generateNonceStr());
//        String system = paramMap.get("system");
//        if (StringUtils.isNotEmpty(system) && system.equals(CallSystem.GROUPON.getCodeStr())) {
//            wxpayRequestBody.put("body", WxPayConfig.GROUPON_GOODS_NAME);
//        } else {
        wxpayRequestBody.put("body", WxPayConfig.GOODS_NAME);
//        }

        wxpayRequestBody.put("out_trade_no", paramMap.get("payPaymentNo"));
        wxpayRequestBody.put("total_fee", BigDecimalUtils.yuanToFen(paramMap.get("payAmount")).toString());
        // 终端ip
        wxpayRequestBody.put("spbill_create_ip", WxPayConfig.IP);
        wxpayRequestBody.put("auth_code", paramMap.get("auth_code"));

        logger.info("申请授权数据 : " + JSON.toJSONString(wxpayRequestBody));
        // 签名
        wxpayRequestBody.put("sign", WxSignature.getNewSign(wxpayRequestBody, wxpaykey));

        logger.info("带签名,申请授权数据 : " + JSON.toJSONString(wxpayRequestBody));

        String wxpayRequestXml = XmlUtil.toXml(wxpayRequestBody);
        logger.info("请求微信下单,参数:" + wxpayRequestXml);
        String wxpayResponseXml = HttpClientUtils.doPostXml(WxPayConfig.PAY_CODE_API, wxpayRequestXml);
        if (StringUtils.isBlank(wxpayResponseXml)) {
            throw new BusinessException(ExceptionStatus.E2001005.getCode(), ExceptionStatus.E2001005.getMessage());
        }

        WxPayMicropayResult result = WxPayBaseResult.fromXML(wxpayResponseXml, WxPayMicropayResult.class);

        logger.info(wxpayRequestBody.get("out_refund_no") + "-WxPayMicropayResult is " + result);

        result.checkResult(wxpaykey);

        WxPaySmResponse wxPaySmResponse = new WxPaySmResponse();
        BeanUtils.copyProperties(result, wxPaySmResponse);

        logger.info("微信下单返回:" + JSON.toJSONString(wxPaySmResponse));

        return wxPaySmResponse;

    }
}
